<?php
Yii::import('ext.selectdiv.selectdivWidget');
class quickGoldWidget extends selectdivWidget {

    public $formAction = "";
    public $inputId = "";

    public function run() {
        //注册需要的js
        parent::run();
        //单选多选按钮判断  $vo->linkurl   $vo->imgurl
        ?>
        <form action="<?php echo Yii::app()->createAbsoluteUrl('order/order/addCart') ?>" method="post">
            <table width="100%" cellspacing="0" cellpadding="0" border="0">
                <tbody>
                    <tr>
                        <td width="10%" height="35" class="from_1">Game:</td>
                        <td width="31%">
                            <select id="select-game" name="game" onChange="changeGame(this)">
                                <?php
                                $cdbc = new CDbCriteria();
                                $cdbc->compare('product_model', 'GOLD');
                                $cdbc->compare('pid', '<>0');
                                $cdbc->order = "sort ASC";
                                $cdbc->compare('site_id', Yii::app()->params->site_id);
                                $data = SubsiteCategory::model()->findAll($cdbc);
                                $i = 0;
                                foreach ($data as $sdata) {
                                    $i++;
                                    if ($i == 1) {//为下面初始化服务器
                                        $gameid = $sdata->game_id;
                                    }
                                    echo "<option value='" . $sdata->game_id . "'>" . $sdata->title . "</option>";
                                }
                                ?>
                            </select>
                        </td>
                        <td width="10%" class="from_1">Server:</td>
                        <td width="49%"  colspan="2">
                            <input name ="serverText" type="text" id="serverText">
                            <input name ="server"  type="hidden" id="serverText-value">
                        </td>
                    </tr>
                    <tr>
                        <td height="35" class="from_1">Product:</td>
                        <td>
                            <input name ="productext" type="text" id="productext">
                            <input name ="product" type="hidden" id="productext-value">
                        </td>
                        <td class="from_1">Price:</td>
                        <td width="60px" id="product_price"></td>
                        <td>
                            <input type="hidden"  value="GOLD" name="product_type"  />
                            <input type="button" rel="add" class="add-to-cart getAttr" value="Add To Cart"  id="addtocart"/>
                            <input type="button" rel="buy" class="buy-now getAttr" value="Buy Now"  id="buy_now" />
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
        <div id="games" class="selectItemhidden">
            <div id="selectItemAd" class="selectItemtit bgc_ccc"> 
                <h2 id="selectItemTitle" class="selectItemleft"></h2> 
                <div id="selectItemClose" class="selectItemright">×</div>
            </div>
            <div id="selectItemCount" class="selectItemcont"> 
                <div id="selectSub" class="fitler-list"> 
                    <ul class="sub_li">
                        <?php
                        $cdbc = new CDbCriteria();
                        $cdbc->compare('parentid', $gameid);
                        $cdbc->order = "sort ASC";
                        $data = ProductCategory::model()->findAll($cdbc);
                        foreach ($data as $sdata) {
                            $fliterClass = strtoupper(substr($sdata->title, 0, 1));
                            echo "<li  class='" . $fliterClass . "'><a rel='" . $sdata->id . "' title='" . $sdata->title . "'>" . $sdata->title . "</a></li>";
                        }
                        ?>
                    </ul>
                </div>
            </div>
        </div>
        <div id="product" class="selectItemhidden">
            <div id="selectItemAd" class="selectItemtit bgc_ccc"> 
                <h2 id="selectItemTitle" class="selectItemleft"></h2> 
                <div id="selectItemClose" class="selectItemright">×</div>
            </div>
            <div id="selectItemCount" class="selectItemcont"> 
                <div id="p_selectSub" class="fitler-list"> 
                    <ul class="sub_li">
                        <?php
                        $cdbc = new CDbCriteria();
                        $cdbc->compare('parentid', $gameid);
                         $cdbc->order = "sort ASC";
                        $data = ProductCategory::model()->findAll($cdbc);
                        foreach ($data as $sdata) {
                            $fliterClass = strtoupper(substr($sdata->title, 0, 1));
                            echo "<li  class='" . $fliterClass . "'><a rel='" . $sdata->id . "' title='" . $sdata->title . "'>" . $sdata->title . "</a></li>";
                        }
                        ?>
                    </ul>
                    <div class="clearfix"></div>
                </div>
                <div class="clearfix"></div>
            </div>
        </div>
        <?php
        $scriptStr = '$("#serverText").selectCity("#games");setFilter("selectSub")';
        //改变游戏操作
        $zUrl = Yii::app()->createUrl('product/getGameZone');
        $pUrl = Yii::app()->createUrl('product/getGoldProduct');
        $scriptStr.= '
                function changeGame(obj){
                    var value = $(obj).val();
                    $("#serverText").addClass("loading");
                    $.get("' . $zUrl . '",{gid:value},function(data){
                        if(data){
                            $("#selectSub").html(data);
                            $("#serverText").selectCity("#games");
                            setFilter("selectSub");
                            $("#serverText").removeClass("loading");
                            $("#serverText").focus(); 
                        }
                    });
                }
                function setGameVal(_seft,obj){
                    if($(_seft).attr("id") =="serverText"){
                        $(_seft).val($(obj).text());
                        $("#"+ $(_seft).attr("id")+"-value").val($(obj).attr("rel"));
                        var gid = $("#select-game").val();
                         $("#productext").addClass("loading");
                         $.get("' . $pUrl . '",{gid:gid,zid:$(obj).attr("rel")},function(data){
                            if(data){
                                $("#p_selectSub").html(data);
                                $("#productext").selectCity("#product");
                                $("#productext").removeClass("loading");
                                $("#productext").focus();
                            }
                        });
                    }else{
                        $(_seft).val($(obj).attr("title"));
                        $("#"+ $(_seft).attr("id")+"-value").val($(obj).attr("rel"));
                        $("#product_price").html($(obj).attr("price"));
                    }
                }
         ';
        Yii::app()->clientScript->registerScript('main', $scriptStr, CClientScript::POS_END);
    }

}